/*
 * Copyright 2024 Hu ZePing
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gewu.bluetoothflow.adapter;


import android.content.Context;
import android.content.res.ColorStateList;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.gewu.bluetoothflow.R;
import com.gewu.bluetoothflow.db.DeviceInfoBean;
import com.gewu.bluetoothflow.view.SwipeMenuAdapter;


import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * Created by YOLANDA on 2024/12/26.
 */
public class MenuAdapter extends SwipeMenuAdapter<MenuAdapter.DefaultViewHolder> {

    private List<DeviceInfoBean> titles;

    private OnItemClickListener mOnItemClickListener;

    public MenuAdapter(List<DeviceInfoBean> titles) {
        this.titles = titles;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return titles == null ? 0 : titles.size();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
    }

    @Override
    public DefaultViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new DefaultViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(DefaultViewHolder holder, int position) {
        DeviceInfoBean bean = titles.get(position);
        holder.setData(bean.getName());
        holder.setConect(bean.getEnableConnect(),holder.itemView.getContext());
        holder.setTime(bean.getLastTime());
        holder.setOnItemClickListener(mOnItemClickListener);
    }

    static class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tvTitle;
        TextView tvConnect;
        TextView tvConnTime;
        ImageView ivConnect;
        OnItemClickListener mOnItemClickListener;
        private SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.CHINA);
        public DefaultViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_name);
            tvConnect = (TextView) itemView.findViewById(R.id.dev_state);
            ivConnect = (ImageView) itemView.findViewById(R.id.iv_status);
            tvConnTime = (TextView) itemView.findViewById(R.id.tv_time);
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        public void setData(String title) {
            this.tvTitle.setText(title);
        }
        public void setTime(Long time){
            this.tvConnTime.setText(dateFormat.format(time));
        }
        public void setConect(Boolean enable, Context mcontext){
            if (enable){
                ColorStateList colorStateList  = mcontext.getResources().getColorStateList(R.color.green_normal);
                this.tvConnect.setTextColor(colorStateList);
                this.tvConnect.setText(R.string.connectable);
                this.ivConnect.setBackgroundResource(R.drawable.ok);
            }else {
                ColorStateList colorStateList  = mcontext.getResources().getColorStateList(R.color.red);
                this.tvConnect.setTextColor(colorStateList);
                this.tvConnect.setText(R.string.cannot_connect);
                this.ivConnect.setBackgroundResource(R.drawable.error);
            }

        }


        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }

}
